import re
from django.http import HttpResponseRedirect

class MobileMiddleware(object):
    def process_request(self, request):
        
        try:
            referer = request.META['HTTP_USER_AGENT']
        except:
            referer = "Unknown"
            
        BROWSER_REXP = (
            #iPhone
            (r'(.*iPhone.*|.*iPod.*)',u'IPHONE'),
        
            #Android
            (r'.*Android.*',u'ANDROID'),
        
            #Blackberry
            (r'.*BlackBerry.*',u'BLACKBERRY'),
        
            #Opera Mini
            (r'(.*Opera Mini\/.*|.*Opera Mini .*)',u'OPERAMINI'),
        )
        
        for rexp,browser in BROWSER_REXP:
            if re.match(rexp,referer):
                return HttpResponseRedirect('http://m.infocarretera.com')
                
        return None